import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/models/user/xindong_chat_user.dart';
import 'package:xindong/widgets/xindong_avatar.dart';
import 'package:xindong/widgets/xindong_count_red_dots.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

class XinDongChatMessageCell extends StatelessWidget {
  final XinDongChatUser model;
  final Function(XinDongChatUser model) onTap; // 删除
  final Function(XinDongChatUser model) onDelete; // 删除
  const XinDongChatMessageCell({Key? key, required this.model, required this.onTap, required this.onDelete})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => onTap(model),
      behavior: HitTestBehavior.opaque,
      child: Slidable(
        endActionPane: ActionPane(
          extentRatio: 0.5,
          motion: const DrawerMotion(),
          children: _defaultSlideBuilder(),
        ),
        child: _buildConversationItem(),
      ),
    );
  }

  Widget _buildConversationItem() {
    return Container(
      color: model.top == 1 ? DZColors.mainLightColor : DZColors.cellColor,
      padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
      margin: const EdgeInsets.only(bottom: 0.5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 头像
          Stack(
            clipBehavior: Clip.none,
            children: [
              XinDongAvatar(url: '${model.sumb_headimg}', size: 50),

              // 未读数量
              // Positioned(
              //   top: -3,
              //   left: 40,
              //   child: XinDongUnreadCountWidget(count: model.count, height: 16, fontSize: 10),
              // ),
            ],
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    // 昵称
                    Expanded(
                      child: Text(
                        model.getName,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.left,
                        style: const TextStyle(color: DZColors.titleColor, fontSize: 15, fontWeight: FontWeight.w600),
                      ),
                    ),

                    // 时间
                    Text(
                      model.city ?? "",
                      style: const TextStyle(fontSize: 13, color: DZColors.placeholderColor),
                    ),
                  ],
                ),
                const SizedBox(height: 5),
                SizedBox(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      // 类型
                      Expanded(
                        child: Text(
                          model.content ?? "",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.left,
                          style: const TextStyle(
                              fontSize: 12, fontWeight: FontWeight.normal, color: DZColors.contentColor),
                        ),
                      ),
                      // 时间
                      Text(
                        model.addtime ?? "",
                        style: const TextStyle(fontSize: 13, color: DZColors.placeholderColor),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  List<ConversationItemSlidePanel> _defaultSlideBuilder() {
    return [
      ConversationItemSlidePanel(
        onPressed: (context) => onDelete(model),
        backgroundColor: Colors.red,
        foregroundColor: Colors.white,
        label: "删除",
        spacing: 0,
        autoClose: true,
      ),
    ];
  }
}
